/*Copyright (c) 2009 Michael Streif
 *
 *Permission is hereby granted, free of charge, to any person obtaining a copy
 *of this software and associated documentation files (the "Software"), to deal
 *in the Software without restriction, including without limitation the rights
 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *copies of the Software, and to permit persons to whom the Software is
 *furnished to do so, subject to the following conditions:
 *
 *The above copyright notice and this permission notice shall be included in
 *all copies or substantial portions of the Software.
 *
 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *THE SOFTWARE.
 */

#include "idt.h"
#include "console.h"
#include "asm.h"

typedef struct
{
	unsigned short    targetOffsetLow;
	unsigned short    targetSelector;
	unsigned short    flags;
	unsigned short    targetOffsetMiddle;
	unsigned int	targetOffsetHigh;
	unsigned int	reserved;

} __attribute__((__packed__)) IDTDescriptor;

typedef struct
{
	unsigned short    idtLimit;
	unsigned long     idtBaseAddr;

} __attribute__((__packed__)) IDTPointer;

IDTDescriptor idt [256];
IDTPointer idtPtr;

extern void exep0();
extern void exep1();
extern void exep2();
extern void exep3();
extern void exep4();
extern void exep5();
extern void exep6();
extern void exep7();
extern void exep8();
extern void exep9();
extern void exep10();
extern void exep11();
extern void exep12();
extern void exep13();
extern void exep14();
extern void exep15();
extern void exep16();
extern void exep17();
extern void exep18();
extern void exep19();

extern void irq1();
extern void test();

void setupIDT()
{
	unsigned int* mem = (unsigned int*) idt;

	for (int i = 0; i < 256 * 4; i++)
	{
		*mem++ = 0;
	}

	createInterruptDescriptor(0, (unsigned long) &exep0, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(1, (unsigned long) &exep1, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(2, (unsigned long) &exep2, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(3, (unsigned long) &exep3, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(4, (unsigned long) &exep4, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(5, (unsigned long) &exep5, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(6, (unsigned long) &exep6, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(7, (unsigned long) &exep7, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(8, (unsigned long) &exep8, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(9, (unsigned long) &exep9, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(10, (unsigned long) &exep10, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(11, (unsigned long) &exep11, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(12, (unsigned long) &exep12, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(13, (unsigned long) &exep13, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(14, (unsigned long) &exep14, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(15, (unsigned long) &exep15, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(16, (unsigned long) &exep16, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(17, (unsigned long) &exep17, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(18, (unsigned long) &exep18, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(19, (unsigned long) &exep19, 0x08, IDESC_FLAGS);

	//createInterruptDescriptor(0x20, (unsigned long) &irq1, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(0x21, (unsigned long) &irq1, 0x08, IDESC_FLAGS);
	/*createInterruptDescriptor(0x22, (unsigned long) &irq1, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(0x23, (unsigned long) &irq1, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(0x24, (unsigned long) &irq1, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(0x25, (unsigned long) &irq1, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(0x26, (unsigned long) &irq1, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(0x27, (unsigned long) &irq1, 0x08, IDESC_FLAGS);
	createInterruptDescriptor(0x28, (unsigned long) &irq1, 0x08, IDESC_FLAGS);*/

	createInterruptDescriptor(0x40, (unsigned long) &test, 0x08, IDESC_FLAGS);

	idtPtr.idtLimit = (sizeof(IDTDescriptor) * 256) - 1;
	idtPtr.idtBaseAddr = (unsigned long) idt;

	loadIDT((unsigned long*) (&idtPtr));
}

void createInterruptDescriptor(unsigned char intNum, unsigned long targetOffset, unsigned short targetSelector, unsigned short flags)
{
	idt[intNum].targetOffsetLow = (unsigned short) targetOffset;
	idt[intNum].targetSelector = targetSelector;
	idt[intNum].flags = flags;
	idt[intNum].targetOffsetMiddle =  (unsigned short) (targetOffset >> 16);
	idt[intNum].targetOffsetHigh =  (unsigned int) (targetOffset >> 32);
	idt[intNum].reserved = 0;
}
